var app = getApp();
var url = app.url
Page({
  data: {
    name: '',
    password: '',
    phone:""
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if(wx.getStorageSync("phone")){
      this.setData({phone:wx.getStorageSync("phone")})
    }
    
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  bindInputName: function (e) {
    this.setData({ name: e.detail.value })
  },
  bindInputPwd: function (e) {
    this.setData({ password: e.detail.value })
  },
  showTip: function (e) {
    wx.showModal({ 
      content: "若当前账号不存在或密码错误，请在电脑网页端完成修改密码",
      showCancel: false
    })
  },
  bindToLogin: function () {
    var that = this
    // 登录验证
    var name = that.data.name
    if (name == '' || name == null) {
      wx.showModal({ title: '提示', content: "请输入用户名", showCancel: false })
      return
    }
    var pwd = that.data.password
    if (pwd == '' || pwd == null) {
      wx.showModal({ title: '提示', content: "请输入密码", showCancel: false })
      return
    }

    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    var params = { username: name, password: pwd ,phone:this.data.phone}
    console.log(params)
    var encodingStr = app.ecbEncrypto(params)
    wx.request({
      url: url + '/api/account/sysLogin.htm',
      data: encodingStr,
      method: 'POST',
      header: header,
      success: function (res) {
        
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        console.log(rest)
        if (rest.res == 0) {
          // 用户名存入缓存
          wx.setStorageSync('userName', name)
          wx.setStorageSync('nickname', rest.userName)
          wx.setStorageSync('deptName', rest.deptName)
          wx.setStorageSync('deptId', rest.deptId)
          wx.setStorageSync('treamId', rest.treamId)
          wx.setStorageSync('userId', rest.userId)
          wx.setStorageSync('roleId', rest.roleId)
          app.userName = rest.userName;
          wx.navigateBack()
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
      }
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.login({
        success(res) {
          console.log(res);
          var code = res.code
          wx.request({
            url: url + '/api/account/getWxInfo.htm',
            method: "post",
            data: app.ecbEncrypto({code}),
            success: function (res) {
              var decodingStr = app.ecbDecrypto(res.data)
              var rest = JSON.parse(decodingStr)
              rest = JSON.parse(rest.data.data)
              console.log(rest);
              var sendData = {
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                sessionKey: rest.session_key,
              }
              console.log(sendData)
              wx.request({
                url: url + '/api/account/getPhoneNumber.htm',
                data: app.ecbEncrypto(sendData),
                method: "post",
                success: function (res) {
                  var decodingStr = app.ecbDecrypto(res.data)
                  var restData = JSON.parse(decodingStr)
                  if(restData.res){
                    that.setData({phone:restData.res})
                    wx.setStorageSync("phone",restData.res)
                  }else{
                    wx.showToast({
                      title: '授权失败',
                      image: '../../images/error.png',
                      duration: 1000,
                      mask: true
                    })
                  }
                  console.log(restData);
                }
              })
            }
          })
        }
      })
      
    }
  },
});